/*子应用*/
const express = require('express');

const query = require('../pool');

const vertify=require('../modules/token');

const card = express.Router();

// 添加商品到购物车
card.get('/add',async(req,res)=>{
  let obj = req.query;
  let uid=vertify.verifytoken(obj.token);
  if(!uid.uid){
  //console.log("用户名不能为空1")
  res.send({code:0,msg: "用户登录状态已过期"})
  }else{
    let result1=await query(`select * from cake_card where product_id=${obj.product_id} and user_id=${uid.uid}`);
    if(result1.length){
      let newcount=result1[0].count+1;
      result3=await query(`update cake_card set count=${newcount} where product_id=${obj.product_id} and user_id=${uid.uid}`)
    }else{
      let result2=await query(`insert into cake_card values (null,${uid.uid},${obj.product_id},1,1,0,0,0,'填写生日信息，免费赠送生日牌');`)
    }
    let result4=await query(`select cake_card.gid,cake_card.bless_checked,cake_card.bless_word,cake_card.box_checked,cake_card.size_id,cake_card.count,cake_card.checked,cakelist.cid,cakelist.cakename,cakelist.cake_price,cakelist.cake_pic,cakelist.sold_count,cakelist.family,cake_size.tableware from cake_card,cakelist,cake_size where cake_card.product_id=cakelist.cid and cake_card.size_id=cake_size.sid and cake_card.user_id=${uid.uid} order by cake_card.gid desc;`);
    if(result4.length!=0){
      res.send({code:1,data:result4})
    }
  }
})

// 删除购物车的商品
card.get("/del?:params",async(req,res)=>{
  let obj=req.query;
  let uid=vertify.verifytoken(obj.token)
  if(!uid.uid){
  //console.log("用户名不能为空2")
  }else{
    let sql = 'delete from cake_card where product_id=? and user_id=?'
    let params = [obj.product_id,uid.uid];
    let result=await query(sql,params)
    // console.log(result)
    if(result.affectedRows==0){
      res.send({code:0,msg:"删除失败"})
    }else{
      res.send({code:1,msg:"删除成功"})
    }
  }
})

// 查看购物车内的商品
card.get('/list',async(req,res)=>{
  let obj=req.query;
  let uid=vertify.verifytoken(obj.token);
  if(!uid.uid){
  //console.log("用户名不能为空3")
  }else{
    let result =await query(`select cake_card.gid,cake_card.bless_checked,cake_card.bless_word,cake_card.box_checked,cake_card.size_id,cake_card.count,cake_card.checked,cakelist.cid,cakelist.cakename,cakelist.cake_price,cakelist.cake_pic,cakelist.sold_count,cakelist.family,cake_size.tableware from cake_card,cakelist,cake_size where cake_card.product_id=cakelist.cid and cake_card.size_id=cake_size.sid and cake_card.user_id=${uid.uid};`);
    if(!result){
      res.send({code:0,msg:'未查询到相关结果'})
    }else{
      res.send({code:1,msg:'搜索成功',data:result})
    }
  }
});

//获取购物车配件
card.get('/parts',async(req,res)=>{
  let result =await query(`select * from cakelist where cakelist.family="配件" order by cakelist.cid asc;` 
  );
  if(!result.length){
    res.send({code:0,msg:'未查询到相关结果'})
  }else{
    res.send({code:1,msg:'搜索成功',data:result})
  }
});

// 提交订单前更新购物车表内的数据
card.get('/update',async(req,res)=>{
  let obj=req.query;
//console.log(obj)
  let result=await query(`update cake_card set count=${obj.count},checked=${obj.checked},box_checked=${obj.box_checked},bless_checked=${obj.bless_checked},bless_word='${obj.bless_word}' where gid=${obj.gid}`);
  if(result.affectedRows!=0){
    res.send({code:1,msg:"修改成功"})
  }
})

// 提交订单
card.get('/settle',async(req,res)=>{
  let obj=req.query;
  // console.log(obj)
  let uid=vertify.verifytoken(obj[0]);
  if(!uid.uid){
  //console.log('用户名不能为空4')
  }else{
    // console.log(uid);
    let result1=await query(`delete from cake_card where checked=1 and user_id=${uid.uid}`);
    // console.log(result1);
    if(result1.affectedRows!=0){
      let result2=await query(`select cake_card.gid,cake_card.bless_checked,cake_card.bless_word,cake_card.box_checked,cake_card.size_id,cake_card.count,cake_card.checked,cakelist.cid,cakelist.cakename,cakelist.cake_price,cakelist.cake_pic,cakelist.sold_count,cakelist.family,cake_size.tableware from cake_card,cakelist,cake_size where cake_card.product_id=cakelist.cid and cake_card.size_id=cake_size.sid and cake_card.user_id=${uid.uid};`)
      res.send({code:1,data:result2});
    }
  }
})

//暴露
module.exports = card;
